var ns;
var app = new Vue({
    el: '#app',
    data: {
        list: [],
        donelist: [],
        form: {
            ID: 0,
            status: 0, //1-未完成 2-已完成
            StatusName: '全部',
            keywords: ''
        },
        PlanType: 4,
        total: {
            ProcessCount: 0,
            CompleteCount: 0
        },
        TabIndex: 1,
        GPSLocation: '',
        statusList: [
            { "ID": "0", "Name": "全部","count":0  },
            { "ID": "1", "Name": "未完成" ,"count":0 },
            { "ID": "2", "Name": "已完成","count":0  },
        ],
        IsStartStatus:false,
        PlanStartTime:'',
    },
    methods: {
        get_data: function() {
            var that = this;
            var options = {};
            options.action = "get_environment_clean_plan_detail";
            options.ID = that.form.ID;
            options.Status = that.form.status;
            options.Keywords = that.form.keywords;

            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                    that.total = data.total;
                    that.statusList[0].count=that.total.ProcessCount;
                    that.statusList[1].count=that.total.UnStart;
                    that.statusList[2].count=that.total.CompleteCount;

                    //判断状态赋值
                    that.IsStartStatus=data.CanStart;
                    that.PlanStartTime=data.StartTime;
                    if(that.PlanStartTime){
                        that.PlanStartTime=ns.SetTime_style(that.PlanStartTime);
                    }

                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_open_scan: function(item) {
            var that = this;
            ns.confirmPer('camera', function() {
                setTimeout(function() {
                    that.do_open_scan_start(item);
                }, 200)
            })
        },
        do_open_scan_start: function(item) {
            var that = this;
            ns.openFrame('scanner_frm', {
                type: 5,
                id: item.ID,
                EncriptID: item.EncriptID
            });
        },
        open_details: function(item) {
            ns.openWin('bjlh_details_frm', '保洁区域', {
                id: item.ID
            });
        },
        select_status: function(e) {
            // this.TabIndex = status;
            this.form.status =  e.target.value;
            this.get_data();
        },
        onClickStatus:function(item){
            var that = this;
            that.form.status = item.ID;
            that.get_data();
        },
        do_view: function(item) {
            var that = this;
            if (item.RequireScan && item.TaskStatus != 3) {
                that.do_open_scan(item);
                return;
            }
            that.open_details(item);
        },
        clear_searchVal: function() {
            var that = this;
            document.getElementById("search-input").value = '';
        },
        do_search: function() {
            this.get_data();
        },
        check_scan_result:function(id){
            if(!id){
                ns.openFrame('scanner_frm', {
                    type: 5
                });
            }
            else{
                var that=this;
                var options = {};
                options.action = "qrcoderesult";
                options.ID = that.form.ID;
                options.QRCode = id;
                ns.post(options, function(succeed, data, err) {
                    if (succeed) {
                        // 接口判断，如果成功后跳转详情页
                        ns.openWin('bjlh_details_frm', '保洁区域检查', {
                            id: data.TaskID
                        });
                    } else if (err) {
                        api.toast({
                            msg: err,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });
                
            };
        },
        onDealTaskTime:function(time){
            return ns.SetTime_style(time)
        },
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    app.form.ID = api.pageParam.id || 0;
    ns.init_search();
    toast = new auiToast();
    setTimeout(function() {
        app.get_data();
    }, 500)
    api.addEventListener({
        name: 'devicetaskcomplete'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'scandeviceqrcodedone'
    }, function(ret, err) {
        app.check_scan_result(ret.value.id);
    });
}
